import {createRouter, createWebHashHistory} from 'vue-router'
import LoginView from "@/views/LoginView.vue";

const routes = [
    {
        path: '/',
        name: 'home',
        component: () => import("@/views/HomeView.vue"),
        redirect:'/welcome',
        children:[
            {
                path:'/welcome',
                name:'welcome',
                component:() => import('../views/WelcomeView.vue')
            },
            {
                path:'/user_list',
                name:'user_list',
                component:() => import('../views/UserView.vue')
            },
            {
                path:'/role_list',
                name:'role_list',
                component:() => import('../views/MenuView.vue')
            },
            {
                path:'/author_list',
                name:'author_list',
                component:() => import('../views/AuthorView.vue')
            },
            {
                path:'/group_list',
                name:'group_list',
                component:() => import('../views/GroupView.vue')
            },
            {
                path:'/attribute_list',
                name:'attribute_list',
                component:() => import('../views/AttributeView.vue')
            },
            {
                path:'/product_list',
                name:'product_list',
                component:() => import('../views/ProductView.vue')
            },
            {
                path:'/add_product',
                name:'add_product',
                component:() => import('../views/addProductView.vue')
            },
            {
                path:'/order_list',
                name:'order_list',
                component:() => import('../views/orderView.vue')
            },
            {
                path:'/data_list',
                name:'data_list',
                component:() => import('../views/DataView.vue')
            },
        ]
    },
    {
        path: '/login/',
        name: 'login',
        component: LoginView
    },

]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

export default router

//做router跳转的login_required验证
router.beforeEach((to, from, next) => {
    if (to.path === '/login/') {
        next()
    } else {
        //获取token值
        const token = localStorage.getItem('token')
        if (!token) {
            next('/login/')
        }
        next()
    }
})